<template>
  <div>
    <div class="modal-header">
      <div class="modal-title">{{ l('RefreshTokenConfirm') }}</div>
    </div>
    <div class="modal-body">
      <a-statistic-countdown :value="deadline" @finish="onFinish" format="m 分 s 秒">
        <template #prefix>
          <span>会话将在</span>
        </template>
        <template #suffix>
          <span>后过期，是否保持登录</span>
        </template>
      </a-statistic-countdown>
    </div>
    <!-- 功能按钮 -->
    <div class="modal-footer">
      <a-button :disabled="saving" @click="onFinish" nz-button type="button">{{
        l('No')
      }}</a-button>
      <a-button html-type="submit" :loading="saveLoading" @click="refresh" type="primary">{{
        l('Yes')
      }}</a-button>
    </div>
  </div>
</template>

<script lang="ts">
  import { ModalComponentBase } from '../../../shared/component-base';
  import { defineComponent } from 'vue';
  import { abpService } from '/@/shared/abp/';
  import { RefreshTokenResult, RefreshTokenServiceProxy } from '/@/shared/service-proxies';
  import { loginService } from '/@/account/login/login.service';
  import { AppConsts } from '/@/abpPro/AppConsts';
  import { useUserStore } from '../../store/vbenModules/user';
  export default defineComponent({
    mixins: [ModalComponentBase],
    data() {
      return {
        saveLoading: false,
        refreshTokenServiceProxy: new RefreshTokenServiceProxy(),
        deadline: 0,
        userStore: useUserStore(),
      };
    },
    computed: {},
    created() {
      const expireTime = abpService.abp.utils.getCookieValue(
        AppConsts.authorization.authTokenExpireDateTime,
      );
      // 初始化倒计时时间
      this.deadline = new Date(expireTime).getTime();
    },
    methods: {
      onFinish() {
        // 计时结束关闭
        this.close();
        this.userStore.logout();
      },
      refresh() {
        this.saveLoading = true;
        // 获取刷新token
        const refreshToken = abpService.abp.auth.getRefreshToken();
        this.refreshTokenServiceProxy
          .refreshToken(refreshToken)
          .finally(() => {
            this.saveLoading = false;
          })
          .then((result: RefreshTokenResult) => {
            console.log(result);
            if (result) {
              this.saveLoading = true;
              // 重新设置token  重新设置时间
              loginService.setAppAccessToken(
                result.accessToken!,
                result.encryptedAccessToken!,
                result.expireInSeconds,
                result.refreshToken!,
                result.refreshTokenExpireInSeconds,
              );
              this.notify.success(this.l('RefreshTokenSuccessfully'));
              this.success(true);
            }
          });
      },
    },
  });
</script>

<style scoped lang="less"></style>
